<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/25
 * Time: 下午2:09
 */

namespace app\admin\controller;


use think\Controller;

class Log   extends Controller
{
    public function login ()
    {
        if (!is_null(session('name'))) {
            $this->redirect('member/index' );
        }
        return view();
    }

    public function checklogin ()
    {
        $post = input('post.' , '' , 'trim,htmlspecialchars');
//        $admin = validate('admin');
//
//        if (!$admin->check($post)) {
//            $this->error( $admin->getError() );
//            die;
//        }


        $count = db('admin')->where(['username'=>$post['name'] , 'password' => md5($post['pwd']) ])->count();

        if ($count) {
            // 保存用户的信息 (使用session)
            session('name' , $post['name']);
            // 正确
            $this->success('登录成功' , url('member/index'));
        }else {
            // 错误
            $this->error('用户名或密码错误');
        }

    }

    public function logout (){
        // 销毁session
        session('name' , null);

        $this->success('退出成功' , url('login'));
    }
}